You can use this program to export Last.FM playback history to anything. Just run it, supply with your account name and offset page number (or skip this one if you want an export of your entire library).
Add your functionality around lines 114-115 to either write a text file or CSV or push to the database, it's your call really. You are welcome to use it anywhere you want.
Courtesy of Largest Music Library Ever.
package com.aulismedia.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LastFMExport {
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
String account = "";
int startPage = 1;
if (args==null || args.length==0) { account = "davidusher"; } // whatever ;-)
if (args.length>0) { account = args[0]; }
if (args.length>1) { startPage = Integer.parseInt( args[1] ); }
( new LastFMExport() ).processAccount(account, startPage);
}
public void processAccount(String accountName, int startPage) {
String operatingHTML = "";
try {
operatingHTML = getURLContents("http://www.last.fm/user/"; + accountName + "/tracks");
} catch (Exception e2) {
System.out.println( "Could not load initial page." );
System.exit(1);
}
int numberOfPages = 0;
String prefix = "class=\"lastpage\">";
String postfix = "</a>";
Pattern patternNumber = Pattern.compile( prefix + "(\\d+)" + postfix);
Matcher myMatcher = patternNumber.matcher(operatingHTML);
if (!myMatcher.find()) {
System.out.println( "Could not get number of pages" );
System.exit(1);
}
numberOfPages = Integer.parseInt( myMatcher.group().substring(prefix.length(), myMatcher.group().length()-postfix.length()) );
for (int i=1; i<=numberOfPages; i++) {
try {
operatingHTML = getURLContents("http://www.last.fm/user/"; + accountName + "/tracks?page=" + i);
} catch (Exception e1) {
System.out.println( "Page " + i + " could not be loaded." );
System.exit(1);
}
while (operatingHTML.indexOf("<td class=\"subjectCell\">")>-1) {
operatingHTML = operatingHTML.substring( operatingHTML.indexOf("<td class=\"subjectCell\">")+1 );
Pattern valueFinder = Pattern.compile("<a[^>]*>(.*?)</a>");
Matcher regexMatcher = valueFinder.matcher(operatingHTML);
String artistName = null;
String trackTitle = null;
String timeStamp = null;
Date date = null;
int counter = 0;
while(regexMatcher.find() && counter<2) {
String matchedString = regexMatcher.group().substring(regexMatcher.group().indexOf(">")+1 , regexMatcher.group().lastIndexOf("<"));
matchedString = matchedString.replace("&", "&");
if (counter==0) artistName = matchedString;
if (counter==1) trackTitle = matchedString;
counter++;
}
Pattern abbrFinder = Pattern.compile("<abbr[^>]*>(.*?)</abbr>");
Matcher abbrRegexMatcher = abbrFinder.matcher(operatingHTML);
if (abbrRegexMatcher.find()) {
timeStamp = abbrRegexMatcher.group().substring( abbrRegexMatcher.group().indexOf("\"")+1, abbrRegexMatcher.group().lastIndexOf("\"") );
timeStamp = timeStamp.replace("T", " ").replace("Z", "");
try {
date = sdf.parse(timeStamp);
} catch (ParseException e) {
System.out.println( "This should not happen - date parser exception" );
System.exit(1);
}
}
String report = String.format("Imported: %s - %s @ %s", artistName, trackTitle, date.toString());
System.out.println( report );
// here you can do whatever you wish with this data:
// artistName, trackTitle and date
}
System.out.println( "Finished page " + i);
System.out.println( "" );
}
}
public static String getURLContents(String urlString) throws Exception {
StringBuffer results = new StringBuffer();
InputStream is = null;
try {
URL url = new URL(urlString);
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) ");
urlc.setRequestProperty("Content-Type", "text/html; charset=UTF-8" );
urlc.setConnectTimeout(5000);
urlc.setReadTimeout(5000);
is = urlc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
results.append(line).append("\n");
}
} catch (Exception E) {
// E.printStackTrace();
}
finally { if (is != null) is.close(); }
return results.toString();
}
}
This is it
Следующая статья: [ Summary of 2011 ]